package com.example.fitnessservice.service.impl;

import com.example.fitnessservice.module.dao.directormapper.DirectorMapper;
import com.example.fitnessservice.module.pojo.Coach;
import com.example.fitnessservice.module.pojo.CourseDetails;
import com.example.fitnessservice.module.pojo.CourseDetailsCamp;
import com.example.fitnessservice.module.utils.TimeUtils;
import com.example.fitnessservice.service.DirectorService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class DirectorServiceImpl implements DirectorService {

    @Resource
    private DirectorMapper directorMapper;

    /**
     * 查找主管管理的教练，并且查出对应教练的课程
     * @param directorId 场馆Id
     * @return
     */
    @Override
    public List<Coach> findCoach(Integer directorId) {
        return directorMapper.findCoachByDirectorId(directorId);
    }

    /**
     * 给教练添加课程
     * @param courseDetails
     * @return
     */
    @Override
    public boolean addCourseByCoachId(CourseDetails courseDetails) {
        int count = directorMapper.addCourseByCoachId(courseDetails);
        if (count == 1){
            //课程添加成功，就要在教练课程关联表中添加一条数据
            //添加主教练的管理
            directorMapper.addCoachCourse(courseDetails.getCourseCoachId(), courseDetails.getId());

        }

        return count == 1 ? true : false;
    }


    /**
     * 删除教练的课程
     * @param courseId
     * @return
     */
    @Override
    public boolean deleteCourseByCourseId(Integer courseId) {
        int count = directorMapper.deleteCourseByCourseId(courseId);
        directorMapper.deleteCoachCourse(courseId);
        return count == 1 ? true : false;
    }

    /**
     * 修改课程的信息
     * @param courseDetails
     * @return
     */
    @Override
    public boolean updateCourse(CourseDetails courseDetails) {
        //先根据课程id先查询课程的历史记录
        CourseDetails record = directorMapper.findCourseById(courseDetails.getId());

        //判断教练有没有变，如果教练变了，教练课程关联表也要发生改变
        if(record.getCourseCoachId() != courseDetails.getCourseCoachId()){
            //更新教练课程关联表中的信息
            directorMapper.updateCoachCourse(courseDetails.getCourseCoachId(), courseDetails.getId());
        }

        //修改课程表中的信息
        int count = directorMapper.updateCourseDetails(courseDetails);

        return count == 1 ? true : false;
    }

    /**
     * 删除训练营信息
     * @param campId
     * @return
     */
    @Override
    public boolean deleteCamp(Integer campId) {
        int count = directorMapper.deleteCamp(campId);
        return count == 1 ? true : false;
    }

    /**
     * 修改训练营信息
     * @param courseDetailsCamp
     * @return
     */
    @Override
    public boolean updateCamp(CourseDetailsCamp courseDetailsCamp) {
        int count = directorMapper.updateCamp(courseDetailsCamp);
        return count == 1 ? true : false;
    }

    /**
     * 添加训练营信息
     *
     * @param courseDetailsCamp 训练营信息
     * @param frequency         训练营上课的次数
     * @param interval          训练营上课的时间间隔
     * @return
     */
    @Override
    public boolean addCamp(CourseDetailsCamp courseDetailsCamp, Integer frequency, Integer interval) {
        //查询训练营编号的最大值，新添加的训练营编号为数据库中编号的最大值加一
        Integer maxId = directorMapper.findMaxCourseId();
        if (maxId == null) {
            courseDetailsCamp.setCourseId(1);
        } else {
            courseDetailsCamp.setCourseId(maxId + 1);
        }
        //根据训练营上课的次数，就要添加多少条记录
        int count = 0;
        for (int i = 0; i < frequency; i++) {
            CourseDetailsCamp camp = courseDetailsCamp;
            if (i == 0) {
                count += directorMapper.addCamp(camp);
                continue;
            }
            Date startTime = TimeUtils.getNewDate(courseDetailsCamp.getCourseStartTime(), interval);
            camp.setCourseStartTime(startTime);
            Date endTime = TimeUtils.getNewDate(courseDetailsCamp.getCourseEndTime(), interval);
            camp.setCourseEndTime(endTime);
            count += directorMapper.addCamp(camp);

        }

        return count == frequency ? true : false;
    }


}
